package net.fei.scan.quick.adapter

import android.content.Context
import android.content.Intent
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.AdapterView.OnItemSelectedListener
import androidx.recyclerview.widget.RecyclerView
import com.google.gson.Gson
import net.fei.scan.quick.databinding.CreateItemBinding
import net.fei.scan.quick.entity.History
import net.fei.scan.quick.ui.AddActivity

/**
 *Create by Xianban
 *2023/11/8 12:01
 */

class HistoryAdapter(private val context: Context, private val list: List<History>) :
    RecyclerView.Adapter<HistoryAdapter.ViewHolder>() {

    private var index=0


    class ViewHolder(view: CreateItemBinding) : RecyclerView.ViewHolder(view.root) {
        val img = view.img
        val text = view.text
        val view = view.view
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val binding = CreateItemBinding.inflate(LayoutInflater.from(parent.context), parent, false)
        return ViewHolder(binding)
    }

    override fun getItemCount(): Int {
        return list.size
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val history = list[position]
        holder.img.setBackgroundResource(history.image)
        holder.text.text = history.title
        if (position == list.size - 1) {
            holder.view.visibility = View.GONE
        }
        holder.itemView.setOnClickListener {
            val intent = Intent(context, AddActivity::class.java)
            intent.putExtra("history", Gson().toJson(history))
            intent.putExtra("index",position)
            context.startActivity(intent)
        }
    }

}